import java.util.ArrayList;

/**
 * Player class. Each Player has a name, five ships and a Grid. A Player can
 * determine whether he or she has been defeated or not.
 */
public class Player {

	private String name;
	private Ship[] ships;
	private ArrayList<String> used;
	//private Grid grid;
	private ButtonGrid grid;

	/**
	 * Player constructor. Initializes the following three things:
	 * 
	 * 1. Sets the player's name as specified by the name parameter.
	 * 
	 * 2. Initializes the player with their five ships: an AircraftCarrier, a
	 * Battleship, a Submarine, a Destroyer and a PatrolBoat.
	 * 
	 * 3. Creates the Grid for the player.
	 * 
	 * @param name
	 *            The name to be used for this player.
	 */
	public Player(String name) {
		// 1
		this.name = name;

		// 2
		ships = new Ship[] { new AircraftCarrier(), new Battleship(), new Cruiser(),
				new Submarine(), new Destroyer()};

		// 3
		//grid = new Grid(this);
		grid = new ButtonGrid();
	}

	/** 
	 * @return This player's grid. 
	 * */
	public ButtonGrid getGrid() {
		return grid;
	}

	/** 
	 * @return This player's name. 
	 */
	public String getName() {
		return this.name;
	}

	/**
	 * @return The array of this player's ships. The array must always contain
	 *         the ships in this order: AircraftCarrier, Battleship, Submarine,
	 *         Destroyer and PatrolBoat.
	 */
	public Ship[] getShips() {
		return ships;
	}

	/**
	 * Returns true if this player has been defeated when all ships are gone. 
	 * 
	 * @return Returns true if this player has been defeated, false otherwise.
	 */
	public boolean isDefeated() {
		int sunkCount = 0;
		for (int i = 0; i < ships.length; i++) {
			if (ships[i].isSunk()) {
				sunkCount++;
			}
		}
		return sunkCount == ships.length;
	}
}
